Skip to content

Add table and job for daily tracking and recording of wiki metrics#880

Merged
rosalieper merged 7 commits into
mainfrom
T383421
Feb 10, 2025
Merged

Add table and job for daily tracking and recording of wiki metrics#880
rosalieper merged 7 commits into
mainfrom
T383421

Conversation

@rosalieper
Copy link
Copy Markdown
Contributor

@rosalieper rosalieper commented Jan 31, 2025

Added new table wiki_daily_metrics
Added a new Wiki.php class in the metric folder for wiki related metrics
Added a Job scheduled to run daily and update metrics in the wiki_daily_metric table.

Bug: T383421

@rosalieper rosalieper marked this pull request as draft January 31, 2025 14:38
@rosalieper rosalieper force-pushed the T383421 branch 3 times, most recently from bacc211 to 37591b6 Compare February 3, 2025 10:48
Copy link
Copy Markdown
Contributor

@tarrow tarrow left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a few thoughts for you to while over; looks like a great start :)

Comment thread app/Jobs/UpdateWikiMetricDailyJob.php Outdated
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;

class UpdateWikiMetricDailyJob implements ShouldQueue
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I get how we got here but I wonder if we can think of a way to name this or add a comment to make it clearer how this jobs differs from all of the other metric jobs

Comment thread app/Jobs/UpdateWikiMetricDailyJob.php Outdated
$isDeleted = (bool)$this->wiki->deleted_at;
if( !$oldRecord || $oldRecord->pages != $todayPageCount || !$oldRecord->is_deleted ) {
WikiDailyMetric::create([
'id' => $this->wiki->id . '_' . date('Y-m-d'),
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like this is doing exactly what the ticket asked; I wondered for a while if it would be better to have a a primary key that is less of a natural key. See for example https://stackoverflow.com/a/718200

Perhaps you should ask Anton if he's really sure he wants this: for example is he certain he won't want to get two of these in a single day? Would there ever be an issue in merging or splitting wikis in the future?

OTOH I think this isn't a dealbreaker and we can probably live with it either way

Comment thread tests/Jobs/UpdateWikiMetricDailyJobTest.php Outdated
Comment thread app/Jobs/UpdateWikiMetricDailyJob.php Outdated
{
$today = now()->format('Y-m-d');
$oldRecord = WikiDailyMetric::where('wiki_id', $this->wiki->id)->latest('date')->first();
$todayPageCount = $this->wiki->wikiSiteStats()->first()->pages ?? null;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

probably fine to just null this; to note this can happen if the wikiSiteStats update job hasn't run at all for this wiki yet

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure I understand. Do you mean I should set the value of $todayPageCount to null?

Copy link
Copy Markdown
Contributor

@tarrow tarrow Feb 4, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After looking it's not fine to null this since pages claims to be an integer https://github.com/wbstack/api/pull/880/files#diff-516b6ba1298695ebbde7ccd7327da6954e9d29a05b82c3c51898d6fa7ad64a91R17

Comment thread app/Console/Kernel.php Outdated
$wikis = Wiki::all();

foreach ($wikis as $wiki) {
dispatch(new UpdateWikiMetricDailyJob($wiki));
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably fine... I wonder if we're going to find that in production creating 1k+ jobs all at the same time is is problematic; how much worse would you find it to add a job then then spawns the other jobs over a period of time? Maybe we could even add a sleep to this closure here?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One way to go about this would be to loop through the wikis in one job, instead of calling a job per wiki but my issue was how to test that. I wanted a way to inject the wiki in the job for testing.

Comment thread app/Jobs/UpdateWikiMetricDailyJob.php Outdated
WikiDailyMetric::create([
'id' => $this->wiki->id . '_' . date('Y-m-d'),
'pages' => $todayPageCount,
'id_deleted' => $isDeleted,
Copy link
Copy Markdown
Contributor

@tarrow tarrow Feb 4, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I discovered trying to run it that this is a typo

Suggested change
'id_deleted' => $isDeleted,
'is_deleted' => $isDeleted,

Add Job for the above table updates daily

Bug:T383421
@rosalieper rosalieper changed the title Draft Commit Add table and job for daily tracking and recording of wiki metrics Feb 4, 2025
@rosalieper rosalieper marked this pull request as ready for review February 6, 2025 15:17
Comment thread tests/Metrics/WikiMetricsTest.php Outdated
]);
}

/** @test*/
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/** @test*/

Comment thread tests/Metrics/WikiMetricsTest.php Outdated
{
use RefreshDatabase;

/** @test */
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/** @test */

Copy link
Copy Markdown
Contributor

@tarrow tarrow left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks good to me; i'd remove these test annotations since we don't have them anywhere else.

Let's ship this thing and see how it works!

@rosalieper rosalieper merged commit 7b1f1de into main Feb 10, 2025
@rosalieper rosalieper deleted the T383421 branch February 10, 2025 13:28
deer-wmde pushed a commit that referenced this pull request Dec 15, 2025
)

* Add WikiDailyMetric table
Add Job for the above table updates daily

Bug:T383421

* import needed classes in test

* Refactoring: created a wiki class in the metric folde and added tests for it

* added comments

* Remove test annotations

* Update CHANGELOG

* Fix typo in CHANGELOG and tests
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants